setwd("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE")
library(haven)
library(dplyr)
library(janitor)

Jul2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE/1 Jul2020/UCL_DEL_Sandbox_Germany_Wave2_client.sav")
Jul2021 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE/2 Jul2021/UCL_DEL_Sandbox_Germany_July2021_client.sav")
Jun2022 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE/3 Jun2022/UCL_DEL_Sandbox_Germany_June2022_client.sav")
Mar2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE/4 Mar2023/UCL_DEL_Sandbox_Germany_Feb2023_client.sav")
Oct2023 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE/5 Oct2023/UCL_DEL_Sandbox_Germany_Oct2023_client.sav")

DE_comp <- compare_df_cols(Jul2020, Jul2021, Jun2022)

#rename Weighting variables to Weight
Jul2020 <- Jul2020 %>% 
  rename(Weight = WeightSandboxW2)
Jul2021 <- Jul2021 %>% 
  rename(Weight = WeightSandbox)

#Jul2020
names(Jul2020)
##may need to change intcon, conshould, conspirx and g1-g12 (see US)
table(Jul2020$intcon)
table(Jul2020$conshould)
table(Jul2020$conspirx_1)
table(Jul2020$conspirx_2)
table(Jul2020$conspirx_3)
table(Jul2020$conspirx_4)
table(Jul2020$conspirx_5)
table(Jul2020$g9mat_p)

Jul2020 <- Jul2020 %>%
  mutate(intcon = intcon-1,
         conshould = conshould-1)
Jul2020 <- Jul2020 %>%
  mutate(across(conspirx_1:conspirx_5, ~  . -1),
         across(g1wash_p:g12wash_fn, ~  . -1))

#Jul2021
names(Jul2021)
##no other change

#Jun2022
##remove all the _w6
Jun2022 <- Jun2022 %>%
  rename_with(~paste0(sub("_w6*", "", .)), -1)
##recode from 1-6 and 1-7 to 0-5 and 0-6
table(Jun2022$list_control)
table(Jun2022$treat_control)
Jun2022 <- Jun2022 %>%
  mutate(list_control = list_control-1,
         treat_control = treat_control-1)

write_sav(Jul2020, "UCL_DEL_Sandbox_DE_Jul2020_Data.sav")
write_sav(Jul2021, "UCL_DEL_Sandbox_DE_Jul2021_Data.sav")
write_sav(Jun2022, "UCL_DEL_Sandbox_DE_Jun2022_Data.sav")

Jul2020 <- read_sav("UCL_DEL_Sandbox_DE_Jul2020_Data.sav")
Jul2021 <- read_sav("UCL_DEL_Sandbox_DE_Jul2021_Data.sav")
Jun2022 <- read_sav("UCL_DEL_Sandbox_DE_Jun2022_Data.sav")

DE_comp <- compare_df_cols(Jul2020, Jul2021, Jun2022, Mar2023, Oct2023)
names(Mar2023)

Mar2023 <- Mar2023 %>% 
  rename(Weight = WeightSandboxDE,
         BTW_21_Quote_refdk = BTW21_Quote_refdk)

Oct2023 <- Oct2023 %>%
  rename(Weight = weight,
         BTW_21_Quote_refdk = BTW21_Quote_refdk) 

#change value labels from long to short hyphen
attributes(Oct2023$hopeful)
get_value_labels(Oct2023$hopeful)
Oct2023$hopeful <- add_labels(Oct2023$hopeful, labels = c(`10 - Sehr hoffnungsvoll` = 11))
Oct2023$fatigue <- add_labels(Oct2023$fatigue, labels = c(`10 - Ich bin es sehr leid, Bilder wie dieses zu sehen` = 11))
Oct2023$motivated <- add_labels(Oct2023$motivated, labels = c(`10 - Sehr motiviert` = 11))

#recode all to 
Oct2023 <- Oct2023 %>%
  mutate(eff_follow_dy_a = eff_follow_dy_a-1,
         eff_follow_dn_a = eff_follow_dn_a-1,
         eff_follow_dy_b = eff_follow_dy_b-1,
         eff_follow_dn_b = eff_follow_dn_b-1,
         eff_follow_dy_c = eff_follow_dy_c-1,
         eff_follow_dn_c = eff_follow_dn_c-1,
         eff_follow_dy_d = eff_follow_dy_d-1,
         eff_follow_dn_d = eff_follow_dn_d-1,
         eff_follow_ey_e = eff_follow_ey_e-1,
         eff_follow_en_e = eff_follow_en_e-1,
         connect = connect-1,
         hopeful = hopeful-1,
         fatigue = fatigue-1,
         motivated = motivated-1,
         sustain = sustain-1)

#track8_1 and track8_3 to change 12 to 11
Oct2023 <- Oct2023 %>%
  mutate(track8_1 = dplyr::recode(track8_1, '12'=11),
         track8_3 = dplyr::recode(track8_3, '12'=11))

table(Oct2023$track8_1)

Jun2022 <- Jun2022 %>%
  rename(BTW_21_Quote_refdk = q_BTW21_Quote_refdk)

write_sav(Jun2022, "UCL_DEL_Sandbox_DE_Jun2022_Data.sav")
write_sav(Mar2023, "UCL_DEL_Sandbox_DE_Feb2023_Data.sav")
write_sav(Oct2023, "UCL_DEL_Sandbox_DE_Oct2023_Data.sav")

Feb2020 <- read_sav("C:/Users/yuxua/Documents/careers/DEL intern/data deposit/Sandboxes/DE/wave1 feb2020/UCL_DEL_Sandbox_Germany_Wave1_client.sav")
DE_comp <- compare_df_cols(Feb2020, Jul2020)

#rename Weighting variables to Weight
Feb2020 <- Feb2020 %>% 
  rename(Weight = WeightSandboxW1)

#Feb2020
names(Feb2020)

##remove all the _w6
Feb2020 <- Feb2020 %>%
  rename_with(~paste0(sub("_w1*", "", .)), -1)

write_sav(Feb2020, "UCL_DEL_Sandbox_Germany_Wave1_client.sav")
